/**
 * Created by python on 18-12-13.
 */
/**
 * Created by python on 18-12-10.
 */
var vm = new Vue({
    el: '#app1',
    data: {
        delimiters: ['{{', '}}'],
        host: HOST,
        user_id: sessionStorage.user_id || localStorage.user_id,
        token: sessionStorage.token || localStorage.token,
        real_name: '',
        mobile: '',
        password: '',
        personal_web: '',
        email: "",
        set_name: true,
        set_web: true,
        set_email: true,
        set_mobile: true,
        email_error: false,
        set_password: true,
        error_password: false,
        allow1: '',
        allow2: '',
        allow3: '',
        display1: false,
        weixin1: true,
        qq1: true,
        weibo1: true,
        qq: '',
        weixin: '',
        weibo: '',
        pwd_message: '',
        name_message_info: false,

        name_message: '',

        histories: []
    },
    mounted: function () {
        // 判断用户的登录状态
        if (this.user_id && this.token) {
            axios.get(this.host + '/user/', {
                // 向后端传递JWT token的方法
                headers: {
                    'Authorization': 'JWT ' + this.token
                },
                // withCredentials: true,
                responseType: 'json',
            })
                .then(response => {
                    // 加载用户数据
                    this.user_id = response.data.id;
                    this.real_name = response.data.real_name;
                    this.mobile = response.data.mobile;
                    this.email = response.data.email;
                    this.personal_web = response.data.personal_web;
                    this.allow1 = Boolean(response.data.allow1);
                    this.display1=true;
                    this.allow2 = Boolean(response.data.allow2);
                    this.allow3 = Boolean(response.data.allow3);
                    this.qq = response.data.qq;
                    this.weixin = response.data.weixin;
                    this.weibo = response.data.weibo;


                })
                .catch(error => {
                    if (error.response.status == 401 || error.response.status == 403) {
                        location.href = '/person-loginsign.html?next=/person-homepage.html';
                    }
                });
        } else {
            location.href = '/person-loginsign.html?next=/person-homepage.html';
        }


    }

    ,
    methods: {
        check_name: function () {
            var len = this.real_name.length;
            if (len < 2 || len > 16) {
                this.name_message_info = "名字在2到16个字符之间";

            }
            else {
                this.name_message_info = false;
            }

        },
        // 修改名字
        setname: function () {

            this.check_name();
            if (!this.name_message) {

                axios.patch(this.host + '/user/',
                    {real_name: this.real_name},
                    {
                        headers: {
                            'Authorization': 'JWT ' + this.token
                        },
                        responseType: 'json'
                    })
                    .then(response => {
                        this.set_name = true;
                        this.name_message = "名字修改成功";
                        // this.name_message_info=true;
                        // this.real_name=response.data.real_name;
                        console.log(response.data)

                    })
                    .catch(error => {


                    })
            }
        },
        setweb: function () {
            // 以后修改网址
            axios.patch(this.host + '/user/',
                {personal_web: this.personal_web},
                {
                    headers: {
                        'Authorization': 'JWT ' + this.token
                    },
                    responseType: 'json'
                })
                .then(response => {
                    this.set_web = true;

                })
                .catch(error => {
                    alert(error.data);
                });
        }
        ,
        setemail: function () {
            // 先验证在发送
            var re = /^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$/;
            if (re.test(this.email)) {
                this.email_error = false;
            } else {
                this.email_error = true;
                return;
            }
            axios.patch(this.host + '/user/',
                {email: this.email},
                {
                    headers: {
                        'Authorization': 'JWT ' + this.token
                    },
                    responseType: 'json'
                })
                .then(response => {
                    this.set_email = true;

                })
                .catch(error => {
                    alert(error.data);
                });
        }
        ,
        check_mobile: function () {
            var re = /^1[345789]\d{9}$/;
            if (re.test(this.mobile)) {
                this.error_phone = false;
            } else {
                this.error_phone_message = '您输入的手机号格式不正确';
                this.error_phone = true;
            }
            if (this.error_phone == false) {
                axios.get(this.host + '/mobiles/' + this.mobile + '/count/', {
                    responseType: 'json'
                })
                    .then(response => {
                        if (response.data.count > 0) {
                            this.error_phone_message = '手机号已存在';
                            this.error_phone = true;
                        } else {
                            this.error_phone = false;
                        }
                    })
                    .catch(error => {
                        console.log(error.response.data);
                    })
            }


        },
        setmobile: function () {
            //
            axios.patch(this.host + '/user/',
                {mobile: this.mobile},
                {
                    headers: {
                        'Authorization': 'JWT ' + this.token
                    },
                    responseType: 'json'
                })
                .then(response => {
                    this.set_mobile = true;

                })
                .catch(error => {
                    alert(error.data);
                });
        }
        ,
        check_pwd: function () {
            var len = this.password.length;
            if (len < 6 || len > 20) {
                this.error_password = true;
            } else {
                this.error_password = false;
            }
        },
        setpassword: function () {

            axios.patch(this.host + '/user/',
                {password: this.password},
                {
                    headers: {
                        'Authorization': 'JWT ' + this.token
                    },
                    responseType: 'json'
                })
                .then(response => {
                    this.set_password = true;
                    this.pwd_message = "密码修改成功"

                })
                .catch(error => {
                    alert(error.data);
                });
        }
        ,

        set_weixin: function () {

        axios.patch(this.host + '/user/',
            {weixin: this.weixin,
             allow1:1
            },
            {
                headers: {
                    'Authorization': 'JWT ' + this.token
                },
                responseType: 'json'
            })
            .then(response => {
                this.allow1 = true;
                this.display1=true;
                this.weixin1=true;


            })
            .catch(error => {
                alert(error.data);
            });


    }



    },



});